import * as React from 'react';

type StateActions = [
  boolean,
  {
    setTrue: () => void;
    setFalse: () => void;
    setToggle: () => void;
  }
];

/**
 * @param {boolean} initialValue 初始值
 * @returns [bool, { setTrue, setFalse, setToggle }]
 * @member setTrue 将布尔值设置为true
 * @member setFalse 将布尔值设置为false
 * @member setToggle 将布尔值切换
 */

export default function useBoolean(
  initialValue: boolean | (() => boolean)
): StateActions {
  const [bool, setBool] = React.useState<boolean>(initialValue);

  const setTrue = React.useCallback(() => {
    setBool(true);
  }, []);

  const setFalse = React.useCallback(() => {
    setBool(false);
  }, []);

  const setToggle = React.useCallback(() => {
    setBool((prevState) => !prevState);
  }, []);

  return [bool, { setTrue, setFalse, setToggle }];
}
